using System.Drawing;
using GTA;
using GTA.Native;
using NativeUI;

namespace CustomPeds.Menus;

public class PlayerMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.player_Menu, new Point(0, -107));
	private static bool isMenuInitialized;

	public PlayerMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.OnItemSelect += OnItemSelect;
			menu.OnMenuClose += OnMenuClose;
			isMenuInitialized = true;
		}
	}

	public static void InitPlayerMenu()
	{
		menu.Clear();
		menu.SetBannerType(new UIResRectangle());
		menu.AddItem(new UIMenuItem(TextSetting.pg_MenuItem_DressingRoom));
		menu.AddItem(new UIMenuItem(TextSetting.pg_MenuItem_DriveToWaypoint));
		menu.AddItem(new UIMenuItem(TextSetting.pg_MenuItem_Clean));
		menu.AddItem(new UIMenuItem(TextSetting.pg_MenuItem_Settings));
		menu.BindMenuToItem(SettingMenu.menu, menu.MenuItems[3]);
		
		// 只在必要时调用RefreshIndex
		if (Game.Player.Character.IsInVehicle())
		{
			menu.MenuItems[0].Enabled = false;
			menu.MenuItems[1].Enabled = true;
		}
		else
		{
			menu.MenuItems[0].Enabled = true;
			menu.MenuItems[1].Enabled = false;
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		switch (index)
		{
		case 0:
			Game.FadeScreenOut(1);
			DressingScript.InitDressing(Main.Member.P1);
			DressingRoomMenu.InitDressingRoom();
			// 选择子菜单时隐藏当前菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			DressingRoomMenu.menu.Visible = true;
			Game.FadeScreenIn(500);
			break;
		case 1:
			Main.P1.DetectDriverAndWaypoint();
			// 选择功能时隐藏菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		case 2:
			Main.P1.Character.ClearBloodDamage();
			Function.Call(Hash._0x9C720776DAA43E7E, Main.P1.Character);
			Main.P1.Character.ResetVisibleDamage();
			Main.P1.Character.RemoveHelmet(instantly: true);
			if (Main.P1.Character.CurrentVehicle != null && Main.P1.Character.IsInVehicle(Main.P1.Character.CurrentVehicle))
			{
				Main.P1.Character.CurrentVehicle.Wash();
				Main.P1.Character.CurrentVehicle.Repair();
			}
			// 执行功能后隐藏菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		case 3:
			SettingMenu.InitCharacterSettingMenu(Main.Member.P1);
			// 选择子菜单时隐藏当前菜单
			menu.Visible = false;
			Main.OnMainMenuHidden();
			break;
		}
	}

	private void OnMenuClose(UIMenu sender)
	{
	}
}
